package com.nervenets.web.hibernate.dao;

import com.nervenets.web.enumeration.OperateType;
import com.nervenets.web.enumeration.WealthType;
import com.nervenets.web.hibernate.domain.WealthFlow;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public interface WealthFlowDao extends JpaRepository<WealthFlow, Long>, JpaSpecificationExecutor<WealthFlow> {

    WealthFlow findTopByUserIdAndWealthTypeOrderByIdDesc(long userId, WealthType wealthType);

    @Query("select new java.math.BigDecimal(coalesce(sum(offset),0)) from WealthFlow where userId = :userId and wealthType = :wealthType")
    BigDecimal getTotalGet(@Param("userId") long userId, @Param("wealthType") WealthType wealthType);

    @Query("select new java.math.BigDecimal(coalesce(sum(offset),0)) from WealthFlow where userId = :userId and operateType = :operateType and wealthType = :wealthType")
    BigDecimal getTotalByOperateTypeAndWealthType(@Param("userId") long userId, @Param("operateType") OperateType operateType, @Param("wealthType") WealthType wealthType);

    Long countByUserIdAndOperateTypeAndWealthType(long userId, OperateType operateType, WealthType wealthType);
}
